/* eslint-disable new-cap */
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import IconsResolver from 'unplugin-icons/resolver'
import Icons from 'unplugin-icons/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import { defineConfig, loadEnv } from 'vite'
import viteEslint from 'vite-plugin-eslint'

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd())
  console.log(`env`, env)
  return {
    plugins: [
      vue(),
      viteEslint(),

      AutoImport({
        imports: [`vue`],

        resolvers: [
          ElementPlusResolver(),

          IconsResolver({
            prefix: `Icon`,
          }),
        ],
      }),

      Components({
        resolvers: [
          IconsResolver({
            enabledCollections: [`ep`],
          }),

          ElementPlusResolver(),
        ],
      }),

      Icons({
        autoInstall: true,
      }),
    ],
    resolve: {
      alias: {
        '@': resolve(__dirname, `src`),
        api: resolve(__dirname, `src/api`),
        assets: resolve(__dirname, `src/assets`),
        store: resolve(__dirname, `src/store`),
        utils: resolve(__dirname, `src/utils`),
        router: resolve(__dirname, `src/router`),
        hooks: resolve(__dirname, `src/hooks`),
        views: resolve(__dirname, `src/views`),
      },
    },
    base: `./`,
    server: {
      port: 4000,
      open: true,
      cors: true,

      // 设置代理
      proxy: {
        '/api': {
          target: `http://localhost:1000/`,
          changeOrigin: true,
          secure: false,
          rewrite: (path) => path.replace(/^\/api/, ``),
        },
      },
    },
  }
})
